package com.ruoyi.wb.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.wb.domain.WbClassifyYearContent;
import com.ruoyi.wb.domain.WbClassifyYearContentThree;
import com.ruoyi.wb.mapper.WbClassifyYearContentMapper;
import com.ruoyi.wb.service.IWbClassifyYearContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 维保年度类目二级表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class WbClassifyYearContentServiceImpl implements IWbClassifyYearContentService 
{
    @Autowired
    private WbClassifyYearContentMapper wbClassifyYearContentMapper;

    @Autowired
    private WbClassifyYearContentThreeServiceImpl wbClassifyYearContentThreeService;

    /**
     * 查询维保年度类目二级表
     * 
     * @param classifyYearContentId 维保年度类目二级表主键
     * @return 维保年度类目二级表
     */
    @Override
    public WbClassifyYearContent selectWbClassifyYearContentByClassifyYearContentId(Long classifyYearContentId)
    {
        return wbClassifyYearContentMapper.selectWbClassifyYearContentByClassifyYearContentId(classifyYearContentId);
    }

    /**
     * 查询维保年度类目二级表列表
     * 
     * @param wbClassifyYearContent 维保年度类目二级表
     * @return 维保年度类目二级表
     */
    @Override
    public List<WbClassifyYearContent> selectWbClassifyYearContentList(WbClassifyYearContent wbClassifyYearContent)
    {
        return wbClassifyYearContentMapper.selectWbClassifyYearContentList(wbClassifyYearContent);
    }

    /**
     * 新增维保年度类目二级表
     * 
     * @param wbClassifyYearContent 维保年度类目二级表
     * @return 结果
     */
    @Override
    public int insertWbClassifyYearContent(WbClassifyYearContent wbClassifyYearContent)
    {
        wbClassifyYearContent.setCreateTime(DateUtils.getNowDate());
        return wbClassifyYearContentMapper.insertWbClassifyYearContent(wbClassifyYearContent);
    }

    /**
     * 修改维保年度类目二级表
     * 
     * @param wbClassifyYearContent 维保年度类目二级表
     * @return 结果
     */
    @Override
    public int updateWbClassifyYearContent(WbClassifyYearContent wbClassifyYearContent)
    {
        wbClassifyYearContent.setUpdateTime(DateUtils.getNowDate());
        return wbClassifyYearContentMapper.updateWbClassifyYearContent(wbClassifyYearContent);
    }

    /**
     * 批量删除维保年度类目二级表
     * 
     * @param classifyYearContentIds 需要删除的维保年度类目二级表主键
     * @return 结果
     */
    @Override
    public int deleteWbClassifyYearContentByClassifyYearContentIds(Long[] classifyYearContentIds)
    {
        return wbClassifyYearContentMapper.deleteWbClassifyYearContentByClassifyYearContentIds(classifyYearContentIds);
    }

    /**
     * 删除维保年度类目二级表信息
     * 
     * @param classifyYearContentId 维保年度类目二级表主键
     * @return 结果
     */
    @Override
    public int deleteWbClassifyYearContentByClassifyYearContentId(Long classifyYearContentId)
    {
        return wbClassifyYearContentMapper.deleteWbClassifyYearContentByClassifyYearContentId(classifyYearContentId);
    }

    public int deleteWbClassifyYearContentByParentIds(List<Long> parentIds){


        List<WbClassifyYearContent> wbClassifyYearContents = wbClassifyYearContentMapper.selectWbClassifyYearContentListByWbClassifyYearId(parentIds);
        int j=wbClassifyYearContentMapper.deleteWbClassifyYearContentByParentIds(parentIds);
        if (j!=wbClassifyYearContents.size()){
            return 0;
        }

        List<Long> ClassifyContentIds = wbClassifyYearContents.stream().map(WbClassifyYearContent::getClassifyYearContentId).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(ClassifyContentIds)){
            return 1;
        }


       List<WbClassifyYearContentThree>  wbClassifyYearContentThrees=wbClassifyYearContentThreeService.selectWbClassifyYearContentThreeListByClassifyYearContentIds (ClassifyContentIds);
        //删除三级
        int k=wbClassifyYearContentThreeService.deleteWbClassifyYearContentThreeByParentIds(ClassifyContentIds);
        if (k!=wbClassifyYearContentThrees.size()){
            return 0;
        }
        return 1;
    }
}
